package com.tanzeer.deliverytracker.adapter;

import java.util.List;

import com.tanzeer.deliverytracker.R;
import com.tanzeer.deliverytracker.utils.CommonConstants;
import com.tanzeer.deliverytracker.utils.PreferenceConnector;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

public class SettingsListViewAdapter extends ArrayAdapter<String> {

	public Context context;

	public SettingsListViewAdapter(Context context, int textViewResourceId,
			List<String> items) {
		super(context, textViewResourceId, items);
		Log.e("msg", "" + items.size());
		this.context = context;

	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public int getItemViewType(int position) {

		if (position == 0 || position == 1)
			return 0;
		else if (position == 2 || position == 4 || position == 5)
			return 1;
		else
			return 2;
	}

	private class ViewHolder {

		TextView tx1;
		TextView tx2;
		CheckBox chk1;
		Switch swtch1;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		String obj = getItem(position);

		if (convertView == null) {
			int type = getItemViewType(position);
			if (type == 0) {

				convertView = mInflater.inflate(
						R.layout.settings_list_row_two_text, parent, false);
				holder = new ViewHolder();
				holder.tx1 = (TextView) convertView
						.findViewById(R.id.textView1);
				holder.tx2 = (TextView) convertView
						.findViewById(R.id.textView2);

				convertView.setTag(holder);

			} else if (type == 1) {

				convertView = mInflater.inflate(
						R.layout.settings_list_row_two_text_checkbox, parent,
						false);
				holder = new ViewHolder();
				holder.tx1 = (TextView) convertView
						.findViewById(R.id.textView1);
				holder.tx2 = (TextView) convertView
						.findViewById(R.id.textView2);
				int id = Resources.getSystem().getIdentifier(
						"radio_btn_check_holo_light", "drawable", "android");
				holder.chk1 = (CheckBox) convertView
						.findViewById(R.id.checkBox1);
				((CheckBox) convertView.findViewById(R.id.checkBox1))
						.setButtonDrawable(id);

				convertView.setTag(holder);

			} else {
				convertView = mInflater.inflate(
						R.layout.settings_list_row_text_on_off, parent, false);
				holder = new ViewHolder();
				holder.tx1 = (TextView) convertView
						.findViewById(R.id.textView1);
				holder.tx2 = (TextView) convertView
						.findViewById(R.id.textView2);
				holder.swtch1 = (Switch) convertView.findViewById(R.id.switch1);
				convertView.setTag(holder);

			}

		} else
			holder = (ViewHolder) convertView.getTag();

		if (position == 0) {
			holder.tx1.setText("Refresh Interval");
			holder.tx2.setText(PreferenceConnector.readString(context,PreferenceConnector.Interval,"1 Hour (recommended)"));

		} else if (position == 1) {
			holder.tx1.setText("Notification Sound");
			String str = PreferenceConnector.readString(context,
					PreferenceConnector.RingtoneUri, "1");
			if (str.equals("1")) {
				Uri uri = RingtoneManager.getActualDefaultRingtoneUri(context,
						RingtoneManager.TYPE_NOTIFICATION);
				Ringtone ringtone=RingtoneManager.getRingtone(context, uri);
				str=ringtone.getTitle(context);
				if(str.equals(""))
					str="Default sound(Notification)";
			} else

				holder.tx2.setText(str);

		} else if (position == 2) {
			holder.tx1.setText("Vibration");
			holder.tx2.setText("Vibrate when receives push notification");
			holder.chk1.setChecked(PreferenceConnector.readBoolean(context,PreferenceConnector.Vibration,true));
			/*holder.chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
					PreferenceConnector.writeBoolean(context,PreferenceConnector.Vibration,isChecked);
				}
			});*/

		} else if (position == 3) {
			holder.tx1.setText("Silent Mode");
			// holder.tx1.setText("Default Ringtone()");
			holder.swtch1.setChecked(PreferenceConnector.readBoolean(context,PreferenceConnector.Silent_MODE,false));
			/*holder.swtch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
						PreferenceConnector.writeBoolean(context,PreferenceConnector.Silent_MODE,isChecked);
						
					
				}
			});*/

		} else if (position == 4) {
			holder.tx1.setText("Save Tracks");
			holder.chk1.setChecked(PreferenceConnector.readBoolean(context,PreferenceConnector.Save_Track,false));
			/*holder.chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
					PreferenceConnector.writeBoolean(context,PreferenceConnector.Save_Track,isChecked);
				}
			});*/

		} else if (position == 5) {
			holder.tx1.setText("Import Tracks");
			holder.chk1.setChecked(PreferenceConnector.readBoolean(context,PreferenceConnector.Import_Track,false));
		/*	holder.chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
					PreferenceConnector.writeBoolean(context,PreferenceConnector.Import_Track,isChecked);
				}
			});*/

		}

		return convertView;

	}

}
